﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

using PracticeSimple.Models;
using BusinessObjects;

namespace PracticeSimple.Controllers
{
    public class PatientController : Controller
    {
        //
        // GET: /Patient/

        public ActionResult Index(int id)
        {
            PatientModel model = new PatientModel();
            model.patient = DataLayer.GetRepo.Patient().Select(id);
            model.encounters = DataLayer.GetRepo.Encounter().GetEncountersForPatient(id);
            return View(model);
        }

        public ActionResult AddPatient()
        {
            PatientModel model = new PatientModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(PatientModel patientModel)
        {
            Patient patient = new Patient();
            patient.FName = patientModel.tbFName;
            patient.LName = patientModel.tbLName;
            patient.DateOfBirth = Convert.ToDateTime(patientModel.tbDateOfBirth);
            patient.SSN = Convert.ToInt32(patientModel.tbSSN);
            patient.FacilityID = (int)Session["FacilityID"];

            int PatientID = DataLayer.GetRepo.Patient().Save(patient);

            return RedirectToAction("Index", new { id = PatientID });
        }


        public JsonResult SavePatient(string patientJSON)
        {
            Patient patient = new JavaScriptSerializer().Deserialize<Patient>(patientJSON);
            patient.FacilityID = (int)Session["FacilityID"];

            DataLayer.GetRepo.Patient().Save(patient);

            return Json(true);
        }

        public JsonResult AddInsurance(string patientInsuranceJSON)
        {

            PatientInsurance Insurance = new JavaScriptSerializer().Deserialize<PatientInsurance>(patientInsuranceJSON);

            Insurance = DataLayer.GetRepo.PatientInsurance().Save(Insurance);

            return Json(Insurance);
        }

    }
}
